package com.atguigu.lease.app.service.impl;

import com.atguigu.lease.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.app.service.*;
import com.atguigu.lease.common.handler.StgyAppAuthInterceptor;
import com.atguigu.lease.model.bean.GraphInfo;
import com.atguigu.lease.model.bean.LeaseAgreement;
import com.atguigu.lease.model.bean.LeaseTerm;
import com.atguigu.lease.model.bean.UserInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.atguigu.lease.model.vo.GraphVo;
import com.atguigu.lease.model.vo.app.AgreementDetailVo;
import com.atguigu.lease.model.vo.app.AgreementItemVo;
import com.atguigu.lease.model.vo.lease.AggrementItemVo;
import com.atguigu.lease.model.vo.lease.AggrementQueryVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 1
* @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
* @createDate 2025-06-20 14:04:43
*/
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
    implements LeaseAgreementService {
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Override
    public Page<AggrementItemVo> pageItem(Long current, Long size, AggrementQueryVo aggrementQueryVo) {
        return baseMapper.selectAggrementItemVoPage(new Page<AggrementItemVo>(current,size),
                aggrementQueryVo);
    }

    @Override
    public AggrementItemVo getAggrementItemVo(Long id) {
        AggrementItemVo vo = new AggrementItemVo();
        //1、查询并设置租约信息
        LeaseAgreement leaseAgreement = this.getById(id);
        BeanUtils.copyProperties(leaseAgreement,vo);
        //2、查询并设置公寓信息
        vo.setApartmentInfo(apartmentInfoService.getById(leaseAgreement.getApartmentId()));
        //3、查询并设置房间信息
        vo.setRoomInfo(roomInfoService.getById(leaseAgreement.getRoomId()));
        //4、查询并设置支付方式信息
        vo.setPaymentType(paymentTypeService.getById(leaseAgreement.getPaymentTypeId()));
        //5、查询并设置租期信息
        vo.setLeaseTerm(leaseTermService.getById(leaseAgreement.getLeaseTermId()));
        return vo;
    }

    @Override
    public List<AgreementItemVo> listItem() {
        //1、先查询用户的手机号
        UserInfo userInfo = userInfoService
                .getById(StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get().getId());
        //2、查询数据
        return baseMapper.selectAgreementItemVoListItem(userInfo.getPhone());
    }
    //异步编排：
    @Override
    public AgreementDetailVo getDetailById(Long id) {
        AgreementDetailVo vo = new AgreementDetailVo();
        //查询租约
        LeaseAgreement leaseAgreement = this.getById(id);
        BeanUtils.copyProperties(leaseAgreement,vo);
        //查询公寓名称
        String apartmentName = apartmentInfoService.getById(leaseAgreement.getApartmentId()).getName();
        vo.setApartmentName(apartmentName);
        //查询房间号
        String roomNumber = roomInfoService.getById(leaseAgreement.getRoomId()).getRoomNumber();
        vo.setRoomNumber(roomNumber);
        //查询公寓图片列表
        List<GraphVo> apartmentGraphVos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getItemId, leaseAgreement.getApartmentId())
                        .eq(GraphInfo::getItemType, ItemTypeEnum.APARTMENT))
                .stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }).collect(Collectors.toList());
        vo.setApartmentGraphVoList(apartmentGraphVos);
        //查询房间图片列表
        List<GraphVo> roomGraphVos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                        .eq(GraphInfo::getItemId, leaseAgreement.getRoomId())
                        .eq(GraphInfo::getItemType, ItemTypeEnum.ROOM))
                .stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    BeanUtils.copyProperties(graphInfo, graphVo);
                    return graphVo;
                }).collect(Collectors.toList());
        vo.setRoomGraphVoList(roomGraphVos);
        //查询支付方式名称
        String paymentTypeName = paymentTypeService.getById(leaseAgreement.getPaymentTypeId()).getName();
        vo.setPaymentTypeName(paymentTypeName);
        //查询租期单位和租期
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        vo.setLeaseTermUnit(leaseTerm.getUnit());
        vo.setLeaseTermMonthCount(leaseTerm.getUnit());
        //组装数据
        return vo;
    }

    @Override
    public void saveOrUpdateLeaseAgreement(LeaseAgreement leaseAgreement) {
        //根据租期 计算 租约到期时间
        boolean isUpdate = leaseAgreement.getId() != null;
        //查询 租期信息：包含了月数
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());

        Date startDate = new Date();
        Date endDate = new Date();
        if(isUpdate){
            //更新：
            startDate = leaseAgreement.getLeaseStartDate();
            //1、如果租约未到期： 应该在截止日期上+  续约的月数
            if(leaseAgreement.getLeaseEndDate().after(endDate)){
                //租约未到期 进行的续约
                //使用 未到期的时间日期+ 租期的月数
                endDate = new DateTime(leaseAgreement.getLeaseEndDate())
                         .plusMonths(leaseTerm.getMonthCount()).toDate();
            }else{
                //2、如果租约已到期： 应该在当前日期上+ 续约的月数
                endDate = new DateTime() //当前日期+续约的月数
                        .plusMonths(leaseTerm.getMonthCount()).toDate();
            }
        }else{
            //3、如果是新的租约： 应该在当前日期上+ 续约的月数
            endDate = new DateTime() //当前日期+续约的月数
                    .plusMonths(leaseTerm.getMonthCount()).toDate();
        }

        leaseAgreement.setLeaseStartDate(startDate);
        leaseAgreement.setLeaseEndDate(endDate);

        this.saveOrUpdate(leaseAgreement);

    }
    /*
        LocalDateTime: java8的时间工具
                java8以前：SimpleDateFormat
        第三方：
            joda-time：

            hutool:
     */
    public static void main(String[] args) {
        System.out.println(new DateTime().toString("yyyy年MM-dd日 HH"));
        System.out.println(new DateTime(System.currentTimeMillis() - 100000).toDate());

        System.out.println(new DateTime().minusDays(30).toString("yyyy-MM-dd HH:mm:ss"));
        System.out.println(new DateTime().minusMonths(2).toString("yyyy-MM-dd HH:mm:ss"));
        System.out.println(new DateTime().plusHours(5).toString("yyyy-MM-dd HH:mm:ss"));
        System.out.println(new DateTime().plusDays(4).toString("yyyy-MM-dd HH:mm:ss"));
        System.out.println(new DateTime().plusWeeks(3).toString("yyyy-MM-dd HH:mm:ss"));
    }
}




